/**
 * 
 */
package com.base.myproject.client.ui.celleditor;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.widget.form.SimpleComboValue;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.google.gwt.core.client.GWT;

/**
 * 类型是String的下来框，可以在grid和form中使用 form getCombox() grid getCellEditor()
 * 
 * 延时加载
 * 请使用 BaseComboBoxX 
 *2010-1-13
 */
@Deprecated
public class BaseSimpleCombox {
	com.extjs.gxt.ui.client.widget.form.SimpleComboBox<String> sc = null;
	
	boolean b=true;
	/**
	 * 
	 * @param column 字段名
	 * @param tablename 表名
	 * @param filter 过滤条件
	 */
	public BaseSimpleCombox(final String column,final String tablename,final String filter) {
	
		

		sc = new com.extjs.gxt.ui.client.widget.form.SimpleComboBox<String>() {


			public void doQuery(String q, boolean forceAll) {
				if (q == null) {
					q = "";
				}

				FieldEvent fe = new FieldEvent(this);
				fe.setValue(q);
				if (!fireEvent(Events.BeforeQuery, fe)) {
					return;
				}

				if (forceAll) {

				//	store.getLoader().load(getParams(q));
				

				}
				if(b)
				{
				ModelType combotype = new ModelType();
				combotype.setRoot("records");
				combotype.setRecordName("record");
				combotype.setTotalName("totle");
				 combotype.addField("code");
				combotype.addField("value");

				StoreInfo comboinfo = new StoreInfo();
				comboinfo.setTablename("s_service");
				comboinfo.setForcenolimit("yes");
				comboinfo
						.setSql("select "+column+" as value,"+column+" as code from "+tablename+" "+filter);
 
				this.store = new BaseStore(combotype, comboinfo);
				initList();
				
				b=false;
				}
				
				this.store.clearFilters();
				if(q.length()>0)
				{
					this.store.filter(getDisplayField(), q);
				}
				expand();
			}

		
			 public SimpleComboValue<String> findModel(String value) {
//				    SimpleComboValue<String> val = null;
//				    for (SimpleComboValue<String> c : store.getModels()) {
//				      if (c.getValue().equals(value)) {
//				        val = c;
//				        break;
//				      }
//				    }
//			
//				    return val;
				
				    return null;
				 
				  //  return val;
				  }
			 

		};
		sc.setTriggerAction(TriggerAction.ALL);
		sc.setLazyRender(true);
		sc.setForceSelection(true);
	}

	public com.extjs.gxt.ui.client.widget.form.SimpleComboBox<String> getCombox() {
		return sc;
	}

	public CellEditor getCellEditor() {
		CellEditor editor = new CellEditor(sc) {
			@Override
			public Object preProcessValue(Object value) {
				if (value == null) {
					return value;
				}
				return sc.findModel(value.toString());
			}

			@Override
			public Object postProcessValue(Object value) {
				if (value == null) {
					return value;
				}
				return ((ModelData) value).get("value");
			}
		};
		return editor;
	}
	public String getValue()
	{
		
		return sc.getRawValue();
	}
}
